<?php
class TermsTaxonomy extends CActiveRecord {
	/** Taxonomy Types */
	const TAXONOMY_POSTTAG = 'postTag';
	const TAXONOMY_CATEGORY = 'category';
	const TAXONOMY_TOPIC = 'topic';
	
	public $children = array();
	public $level;

	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return strtolower('TermsTaxonomy');
	}
	
	public function rules() {
		return array(
			array('termId, taxonomy', 'required'),
			array('taxonomy', 'length', 'max' => 32)
		);
	}
	
	public function defaultScope() {
		return array(
			'order' => 'taxonomyOrder ASC'
		);
	}
	
	public function scopes() {
		return array(
			'topic' => array(
				'condition' => 'taxonomy = "'.self::TAXONOMY_TOPIC.'"'
			),
			'category' => array(
				'condition' => 'taxonomy = "'.self::TAXONOMY_CATEGORY.'"'
			)
		);
	}
	
	public function relations() {
		return array(
			'terms' => array(self::BELONGS_TO, 'Terms', 'termId'),
			'posts' => array(self::MANY_MANY, 'Posts', 'termsrelationships(subjectId, objectId)'),
			'categoryPic' => array(self::HAS_ONE, 'TermsTaxonomyMeta', 'termTaxonomyId',
//				'together' => true,
				'joinType' => 'LEFT JOIN',
				'condition' => 'metaKey = "'.TermsTaxonomyMeta::KEY_CATEGORY_PIC.'"'
			),
			'topPost' => array(self::HAS_ONE, 'TermsTaxonomyMeta', 'termTaxonomyId',
//				'together' => true,
				'joinType' => 'LEFT JOIN',
				'condition' => 'metaKey = "'.TermsTaxonomyMeta::KEY_TOP_POST.'"'
			),
		);
	}

	/**
	 * Find all categories
	 */
	public function getCategories($rearrange = false, &$count = null, $firstGrade = false) {
		$condition = 'taxonomy = :taxonomy';
		$condition .= $firstGrade === true ? ' AND parent != 0' : ''; 
		$params = array('taxonomy' => self::TAXONOMY_CATEGORY);
		if($count !== null) {
			$count = $this->count($condition, $params);
		}
		return $rearrange ? self::rearrange($this->findAll($condition, $params)) : $this->findAll($condition, $params);
	}

	public static function rearrange(&$categories) {
		foreach($categories as $i => $category) {
			if($category->parent != 0) {
				$categories[$i]->level = 1;
				foreach($categories as $k => $v) {
					if($category->parent == $v->termTaxonomyId) {
						$categories[$k]->level = 0;
						$categories[$k]->children[] = $categories[$i];
					}
				}
				unset($categories[$i]);
			}
		}
		return $categories;
	}
	
	public function attributeLabels() {
		return array(
			'taxonomy' => '名称'
		);
	}

    public function getParent() {
        return self::model()->with('terms')->find('termTaxonomyId = '.$this->parent);
    }

    public function getChildren() {
        return self::model()->with('terms')->findAll('parent = '.$this->termTaxonomyId);
    }

	/**
	 * Find all topics
	 */
	public function getTopics() {
		return $this->findAllByAttributes(array('taxonomy' => self::TAXONOMY_TOPIC));
	}
	
	public function getChildrenCategories($categoryId) {
		return $this->findAllByAttributes(array('parent' => $categoryId));
	}
}
